package com.im.lzj.activity;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.im.lzj.R;

import java.util.List;

/**
 * @author LZJ
 * @date 2019/4/15 10:53
 * @email XiaoMoSilence@outlook.com
 * @Explain
 */
public class GongAdapter extends RecyclerView.Adapter<GongAdapter.ViewHolder> {

    private Context context;
    private List<AnswerBean.ResultBean> list;
    private LayoutInflater inflater;

    public GongAdapter(Context context, List<AnswerBean.ResultBean> list) {
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

    private setItemOnClickChange setItemOnClickChange;

    public void setSetItemOnClickChange(GongAdapter.setItemOnClickChange setItemOnClickChange) {
        this.setItemOnClickChange = setItemOnClickChange;
    }

    public interface setItemOnClickChange {
        void DoClick(View view, int position);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(inflater.inflate(R.layout.item_gride_layout, null));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.mTv_item.setText(list.get(position).getStr());

        if (list.get(position).getType().equals("1")) {
            holder.mRelative.setBackground(context.getResources().getDrawable(R.drawable.shape_answer_f));
            holder.mTv_item.setTextColor(context.getResources().getColor(R.color.black));
        } else {
            holder.mTv_item.setTextColor(context.getResources().getColor(R.color.main1));
            holder.mRelative.setBackground(context.getResources().getDrawable(R.drawable.shape_answer_t));
        }

        holder.mRelative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setItemOnClickChange.DoClick(v, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void RefreshItem(int position) {
        list.get(position).setType("2");
        notifyItemChanged(position);
    }

    public void RefreshItem(String str) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getStr().equals(str)){
                list.get(i).setType("1");
                notifyItemChanged(i);
                return;
            }
        }
    }


    class ViewHolder extends RecyclerView.ViewHolder {

        TextView mTv_item;
        RelativeLayout mRelative;

        public ViewHolder(View itemView) {
            super(itemView);
            mTv_item = itemView.findViewById(R.id.mTv_item);
            mRelative = itemView.findViewById(R.id.mRelative);
        }
    }
}
